package server_msg

import (
	"bytes"
	"encoding/binary"
)

type InternalServerMsg struct {
	GatewayId  uint32
	SessionId  uint32
	UserId     uint32
	Disconnect uint8 //1：断开连接；0：不断开连接（默认值）
	MsgData    []byte
}

func (msg *InternalServerMsg) ToByteArray() []byte {
	buffer := bytes.NewBuffer([]byte{})

	_ = binary.Write(buffer, binary.BigEndian, msg.GatewayId)
	_ = binary.Write(buffer, binary.BigEndian, msg.SessionId)
	_ = binary.Write(buffer, binary.BigEndian, msg.UserId)
	_ = binary.Write(buffer, binary.BigEndian, msg.Disconnect)
	_ = binary.Write(buffer, binary.BigEndian, msg.MsgData)

	return buffer.Bytes()
}

func (msg *InternalServerMsg) FromByteArray(byteArray []byte) {
	if byteArray == nil || len(byteArray) == 0 {
		return
	}

	buffer := bytes.NewBuffer(byteArray)

	_ = binary.Read(buffer, binary.BigEndian, &(msg.GatewayId))
	_ = binary.Read(buffer, binary.BigEndian, &(msg.SessionId))
	_ = binary.Read(buffer, binary.BigEndian, &(msg.UserId))
	_ = binary.Read(buffer, binary.BigEndian, &(msg.Disconnect))
	msg.MsgData = buffer.Bytes()
}
